/**
 * Created by daigou on 2015/5/30.
 */
var http=require('http');
var config=require('./config.json');
var assert=require('assert');

function getProductList(callback){
    var request=http.request(config.apiOption, function (res) {
        var data='';
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            data+=chunk;
        }).on('end', function () {
            data=JSON.parse(data);

            assert(!data.error&&data.result,'product list is empty!');
            console.log('get product list success');

            callback&&callback.call(null,data.result);
        });
    }).on('error', function (e) {
        console.error('call api error,'+ e.message);
    });

    var data={
        method: 'SnapSale.GetProducts',
        id: 65535,
        params: {
            offset: 0,
            limit: 64,
            status: ''
        }
    };

    request.write(JSON.stringify(data),'utf8');

    request.end();
}

module.exports=getProductList;